//
//  HZInitView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/16.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit
import SnapKit

 protocol HZInitViewDelegate:NSObjectProtocol{
    
    
    func register()

}


class HZInitView: UIView {
    
    //定义代理对象(是弱指针类型)
    weak var delegate:HZInitViewDelegate?
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //添加子控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置每一个子控制器有不同的图像
    func setNotLogin(textLab:String?,homeImgName:String?){
        
        if textLab != nil && homeImgName != nil{
            
            rotateImg.hidden=true
            textLabel.text = textLab
            homeImg.image = UIImage(named: homeImgName!)
            return
        }
        
        
        
       //旋转的核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 8
        anim.repeatCount = MAXFLOAT
        //使得切换界面不会关闭核心动画
        anim.removedOnCompletion = false
        
        //加上旋转的核心动画
        rotateImg.layer.addAnimation(anim, forKey: nil)
    }
    
    
    //添加子控件
    private func setupUI(){
    
            //添加子控件
        addSubview(rotateImg)
        addSubview(shadeImg)
        addSubview(homeImg)
        addSubview(textLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //约束
        //        case Left
//        case Right
//        case Top
//        case Bottom
//        case Leading
//        case Trailing
//        case Width
//        case Height
//        case CenterX
//        case CenterY
        
        //约束
        rotateImg.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
        }
        shadeImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(rotateImg.snp_center)
        }
        homeImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(rotateImg.snp_centerX)
            make.top.equalTo(rotateImg.snp_bottom).offset(30)
            make.width.equalTo(200)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(textLabel.snp_leading)
            make.width.equalTo(90)
            make.top.equalTo(textLabel.snp_bottom).offset(10)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(textLabel.snp_trailing)
            make.width.equalTo(90)
            make.top.equalTo(textLabel.snp_bottom).offset(10)
        }

        
        
        //创建定时器
//        let rotateLink = CADisplayLink(target: self, selector: "rotate")
//        
//        //加入到主循环中去
//        rotateLink.addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
        
    }
    

    
  
    
    //懒加载子控件
    lazy var homeImg:UIImageView={
        
        
        let homeImg = UIImageView()
        
        homeImg.image = UIImage(named:"visitordiscover_feed_image_house" )
        
        homeImg.sizeToFit()
        
        return homeImg
        
    }()
    
    
    lazy var shadeImg:UIImageView={
    
        let shadeImg = UIImageView()
        shadeImg.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        shadeImg.sizeToFit()
        
        return shadeImg
        
    }()
    
    
    lazy var rotateImg:UIImageView={
        
        let rotateImg = UIImageView()
        
        rotateImg.image = UIImage(named:"visitordiscover_feed_image_smallicon")
        
        rotateImg.sizeToFit()
        
        return rotateImg
    }()
    
    //哈哈文本
    lazy var textLabel:UILabel={
        
       
        let textLabel = UILabel(color: UIColor.grayColor(), font: 13, title: "水水水水山山水水水水山山水水水水水水",preferredMaxLayoutWidth:200)
     
        return textLabel
    }()
    
    //注册
    lazy var registerBtn:UIButton={
       
        let registerBtn = UIButton(title: "注册", imgName: "common_button_white", titleColor: UIColor.orangeColor(),font:14)
        
        registerBtn.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        
        return registerBtn
    }()

    //登录
    lazy var loginBtn:UIButton={
        
        let loginBtn = UIButton(title: "登录", imgName: "common_button_white", titleColor: UIColor.grayColor(),font:14)
        
        loginBtn.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        
        return loginBtn
    }()
    
    
    // MARK: - 注册按钮点击事件
    @objc private func register(){
        
        //执行代理方法
        delegate?.register()
    }
    
    // MARK: -  登录按钮点击事件
    @objc private func login(){
        
        //发布通知
        NSNotificationCenter.defaultCenter().postNotificationName("login", object: self)
        
    }

}
